package com.lxt.blog.controller;

import com.lxt.blog.dao.UserDao;
import com.lxt.blog.dao.UserDaoImpl;
import com.lxt.blog.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/*
 *用户登录管理类
 */
@WebServlet("/server/login")
public class LoginServlet extends HttpServlet {
    //创建用户默认ID为0,由数据库指定具体ID
    protected static int DEFAULT_ID = 0;
    protected UserDao userDao= new UserDaoImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String key = request.getParameter("key");
        if("login".equals(key)){
            //登录
            login(request,response);
        } else if("checkName".equals(key)){
            //检查用户名是否正确
            checkName(request,response);
        } else if("register".equals(key)){
            //注册用户
            register(request,response);
        } else if(("findPassword").equals(key)){
            //找回密码
            findPassword(request,response);
        }
    }

    /*
     *查找密码
     * 通过root密码和用户名
     * 返回状态和密码
     */
    private void findPassword(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String root = request.getParameter("root");
        String userId = request.getParameter("username");
        String status ="F";
        String password = "LL";
        if("6699".equals(root)){
            password = userDao.selectUserPassword(userId);
            if("LL".equals(password)){
                status="F";
            } else{
                status="T";
            }
        }else {
            status = "F";
        }
        response.getWriter().write("{\"status\":\""+status+"\",\"password\":\""+password+"\"}");
    }

    /*
     *注册
     * 获取用户属性
     * 对属性进行设置
     * 添加到数据库
     * 返回状态
     */
    private void register(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String userId = request.getParameter("username");
        String password = request.getParameter("password");
        String sex = request.getParameter("sex");
        String introduce = request.getParameter("introduce");
        String[] getHobby = request.getParameterValues("hobby");
        String hobby = "";
        for(int i = 0; i < getHobby.length; i++){
            if(!(getHobby[i].equals("LL"))){
                hobby+=getHobby[i]+" ";
            }
        }
        User user = new User(DEFAULT_ID,userId,password,sex,hobby,introduce);
        boolean isRegisterOk = userDao.insertUser(user);
        String registerStatus = isRegisterOk ? "T":"F";
        response.getWriter().write(registerStatus);
    }

    /*
     *登录
     * 获取用户属性
     * 到数据数据库查找
     * 返回状态
     */
    private void login(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String name = request.getParameter("username");
        String password = request.getParameter("password");
        boolean isExist = userDao.selectUser(name,password);
        //如果登录成功,将用户添加到session域中
        String status = "F";
        String url = "";
        if(isExist){
            HttpSession session = request.getSession();
            session.setAttribute("username",name);
            status = "T";
            url = "/../blog/main/main.html";
        }
        String json = "{\"status\":\""+status+"\",\"url\":\""+url+"\"}";
        response.getWriter().println(json);
    }

    /*
     *检查用户姓名
     * 获取用户属性
     * 到数据数据库查找
     * 返回状态
     */
    protected void checkName(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name = request.getParameter("username");
//        System.out.println(name);
        boolean isExist = userDao.selectUser(name,null);
        String status = isExist ? "T":"F";
        response.getWriter().write(status);
    }
}
